package com.lin.service.impl;


import com.lin.entity.PlaneData;
import com.lin.entity.message.ResponseResult;
import com.lin.entity.vo.PlaneDataVo;
import com.lin.entity.vo.PlaneWsVo;
import com.lin.mapper.PlaneDataMapper;
import com.lin.service.PlaneService;
import com.lin.utils.CollectionUtils;
import com.lin.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PlaneServiceImpl implements PlaneService {

    @Autowired
    private PlaneDataMapper planeDataMapper;

    @Override
    public ResponseResult getNewest(HttpServletRequest request) {
//        String token = SecurityUtils.getToken(request);
//        TracePlaneTask tracePlaneTask = token == null ? null : (TracePlaneTask) wsDataHolder.getThreadTask(new WsDataMapKey(ScheduleTask.TRACE_PLANE_TASK.getTaskName(), SecurityUtils.getUserId(token)));

        // 被追踪的目标id
        List<String> tarIds = Arrays.asList("412556000", "413275110", "413251140");

        long s = System.currentTimeMillis();
        Map<String, PlaneData> newPlanes = planeDataMapper.getNewest(tarIds)
                .stream().collect(Collectors.toMap(
                        PlaneData::getTarid,
                        planeData -> planeData,
                        (existing, replacement) -> replacement // 替换重复值tarid
                ));

        long e = System.currentTimeMillis();
        System.out.println("134:" + (e - s));

        // 计算方位角
        Map<String, PlaneData> secondNewPlanes = planeDataMapper.getSecondNew().stream().collect(Collectors.toMap(
                PlaneData::getTarid,
                planeData -> planeData,
                (existing, replacement) -> replacement // 替换重复值tarid
        ));

        List<PlaneWsVo> result = new ArrayList<>(newPlanes.size());
        for (PlaneData newPlane : newPlanes.values()) {
            PlaneWsVo temp = new PlaneWsVo();
            BeanUtils.copyProperties(newPlane, temp);

            PlaneData sendNewPlane = secondNewPlanes.get(newPlane.getTarid());
            if (sendNewPlane != null)
                temp.setAngle(CommonUtils.calculateAngle(sendNewPlane.getLat(), sendNewPlane.getLon(), newPlane.getLat(), newPlane.getLon()));

            result.add(temp);
        }

        // 缓存历史数据
//        wsDataHolder.setHistoryPlanes(result.stream().collect(Collectors.toMap(PlaneWsVo::getTarid, planeWsVo -> planeWsVo)));
        log.info("缓存飞机历史数据");

        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult history(List<String> tarIds) {
        List<PlaneData> historyPlane = planeDataMapper.getRecentByTarids(null, tarIds);
        return ResponseResult.success(historyPlane.stream().collect(Collectors.groupingBy(PlaneData::getTarid)));
    }

    @Override
    public ResponseResult list(PlaneDataVo planeDataVo) {
        return ResponseResult.success(planeDataMapper.list(planeDataVo));
    }

    @Override
    public ResponseResult playback(PlaneDataVo planeDataVo) {
        return ResponseResult.success(planeDataMapper.getRecent(null, planeDataVo.getTarid()));
    }

    @Override
    public ResponseResult getTracePlaneInfo(HttpServletRequest request) {

        List<PlaneData> tracePlanes = new ArrayList<>();
        // 被追踪的目标id
        List<String> tarIds = Arrays.asList("1234", "2343", "2343");
        if (CollectionUtils.isNotEmpty(tarIds)) {
            tracePlanes = planeDataMapper.getByTarId(tarIds);
        }
        return ResponseResult.success(tracePlanes);
    }

    @Override
    public ResponseResult getTraceOnPlayback(Date startTime, String tarid) {
//        List<PlaneData> tracePlaybackCache = wsDataHolder.getTracePlaybackCache();
//        List<PlaneData> traces = tracePlaybackCache.stream()
//                .filter(planeData -> planeData.getTarid().equals(tarid) && planeData.getTarTime().getTime() < startTime.getTime())
//                .limit(50)
//                .collect(Collectors.toList());

        return ResponseResult.success();
    }
}
